﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows;
using CSScriptLibrary;
using MOJsdk;
using IO = System.IO;

namespace MoJScripSuport
{
    public class ScriptSuport : IPlugIn
    {
        public string PluginDeveloper { get { return "Davidblkx"; } }
        public string PluginVersion { get { return "1"; } }
        public string PluginName { get { return "ScriptSuport"; } }
        public string PluginWeb { get { return "http://www.csscript.net"; } }
        public string PluginDescription { get { return "Add suport to C# Scripting language, visit site for details"; } }
        public IEnumerable<string> LinkedFiles
        { get { return new List<string>(); } }

        private string WorkDir
        { get { return IO.Path.Combine(MojFoldersEnum.PluginsFolder.Value(), "Scripts"); } }
        public void Inicialize()
        {
            if (!IO.Directory.Exists(WorkDir))
                IO.Directory.CreateDirectory(WorkDir);

            INIToolButton();

            foreach (var v in GetPlugins())
                if (v.isAuto)
                    v.StartScript(MojHost.GetHost);
        }
        public void Install()
        {

        }
        public void Unistall()
        {

        }
        public void Dispose()
        {
            MojToolButton btn = new MojToolButton("CSS", "CSharpScript_toolButton", "StartScript", null);
            MojHost.GetHost.ToolButtonCollection.Remove(btn);
        }
        private void INIToolButton()
        {
            MojToolButton btn = new MojToolButton("CSS", "CSharpScript_toolButton", "StartScript", null);
            btn.Click += ButtonClicked;
            btn.AddLogicStatment(MojToolButtonShowCondition.ShowAllways);
            MojHost.GetHost.ToolButtonCollection.Add(btn);
        }

        private void ButtonClicked(object sender, RoutedEventArgs e)
        {
            MenuControl _ctrl = BuildControl();
            _ctrl.MouseLeave += delegate
            {
                MojHost.GetHost.HostCanvas.Children.Remove(_ctrl);
            };

            Point pt = Mouse.GetPosition(MojHost.GetHost.HostCanvas);
            MojHost.GetHost.HostCanvas.Children.Add(_ctrl);

            Canvas.SetLeft(_ctrl, pt.X);
            Canvas.SetTop(_ctrl, pt.Y);
            Canvas.SetZIndex(_ctrl, 11);
        }
        private MenuControl BuildControl()
        {
            MenuControl _ctrl = new MenuControl();
            _ctrl.ButtonHeight = 25;
            _ctrl.ButtonWidth = 125;

            foreach (var v in GetPlugins())
            {
                if (v.isAuto) continue;

                Button btn = new Button();
                btn.Content = v.Name;
                btn.ApplyStyle("CSScriptButton");
                btn.Click += StartPlugin;
                _ctrl.AddNewButton(btn);
            }

            return _ctrl;
        }
        private void StartPlugin(object sender, RoutedEventArgs e)
        {
            string name = ((Button)sender).Content.ToString();

            List<IScriptPlugin> list = new List<IScriptPlugin>(GetPlugins());
            if (list.Count(z => z.Name == name && !z.isAuto) > 0)
            {
                list.First(z => z.Name == name && !z.isAuto).StartScript(MojHost.GetHost);
            }
        }

        private IScriptPlugin GetScript(string path)
        {
            try
            {
                string txt = IO.File.ReadAllText(path);
                IScriptPlugin p = CSScript.Evaluator.LoadCode<IScriptPlugin>(txt);
                return p;
            }
            catch
            {
                return null;
            }
        }
        private List<IScriptPlugin> GetPlugins()
        {
            string[] files = IO.Directory.GetFiles(WorkDir, "*.cs");
            List<IScriptPlugin> list = new List<IScriptPlugin>();

            foreach (var v in files)
            {
                IScriptPlugin plug = GetScript(v);
                if (plug != null)
                    list.Add(plug);
            }

            return list;
        }
    }

    public interface IScriptPlugin
    {
        bool isAuto { get; }
        string Name { get; }

        void StartScript(IHost MojHost);
    }
}
